<template>
  <div style="text-align: center">
    <div style="margin-top: 10px;">
      <el-table :data="role"
                border
                stripe
                size="small"
                @selection-change="handleSelectionChange"
                style="width: 100%;">
        <el-table-column
            type="selection"
            width="70">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="70">
        </el-table-column>
        <el-table-column
            prop="roleName"
            label="角色名称"
            width="170">
        </el-table-column>
        <el-table-column
            prop="roleLabel"
            label="角色称号"
            width="170">
          <template slot-scope="scope">
            <el-tag>{{scope.row.roleLabel}}</el-tag>
           </template>
        </el-table-column>
        <el-table-column
            prop="createTime"
            label="创建时间"
            width="170">
        </el-table-column>
        <el-table-column prop="isDisable"
                         label="是否启用"
                         width="170">
          <template slot-scope="scope">
            <el-tag v-if="!scope.row.isDisable" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="showEditView(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="编辑角色"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>角色名称</el-tag>
            </td>
            <td>
              <el-input v-model="updateJl.roleName" size="small"></el-input>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>职称级别</el-tag>
            </td>
            <td>
              <el-select v-model="updateJl.roleLabel"
                         placeholder="角色称号" size="small"
                         style="margin-left: 6px;margin-right:6px">
                <el-option
                    v-for="item in titleLevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <el-switch
                  v-model="updateJl.isDisable"
                  activecolor="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="禁用">
              </el-switch>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PerRoles",
  data() {
    return {
      jl: {
        name: '',
        titleLevel: ''
      },
      role: [],
      updateJl: {
        roleName: '',
        roleLabel: '',
        isDisable: false
      },
      dialogVisible: false,
      multipleSelection: [],
      titleLevels:[
          "admin",
          "user",
          "test"
      ]
    }
  },
  mounted() {
    this.initJls();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },
    showEditView(data) {
      Object.assign(this.updateJl, data);
      this.dialogVisible = true;
    },
    //更新
    doUpdate(){
      this.postRequest('/api/role/updateRole', this.updateJl).then(resp => {
        if (resp) {
          this.initJls();
          this.dialogVisible = false;
        }
      })
    },
    //删除
    deleteHandler(data){
      //删除
      this.$confirm('此操作将永久删除【' + data.roleName + '】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.getRequest('/api/role/roles/list/' +
            data.id).then(resp => {
          if (resp) {
            this.initJls();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //查找方法
    initJls() {
      this.getRequest('/api/role/roles/list').then(resp => {
        if (resp) {
          this.role = resp;
        }
      })
    }
  }
}
</script>

<style scoped>
el-table-column{
  text-align: center;
}

</style>
